package com.xiliubit.cloudpicture.controller;

import com.xiliubit.cloudpicture.annotation.AuthCheck;
import com.xiliubit.cloudpicture.common.BaseResponse;
import com.xiliubit.cloudpicture.common.ResultUtils;
import com.xiliubit.cloudpicture.constant.UserConstant;
import com.xiliubit.cloudpicture.exception.BusinessException;
import com.xiliubit.cloudpicture.exception.ErrorCode;
import com.xiliubit.cloudpicture.manager.CosManager;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;

@RestController
@Slf4j
@Tag(name = "上传接口")
@RequestMapping("/upload")
public class UploadController {

    @Resource
    private CosManager cosManager;

    /**
     * 测试文件上传
     *
     * @param multipartFile
     * @return
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @PostMapping("/test/upload")
    @Operation(summary = "测试上传接口")
    public BaseResponse<String> testUploadFile(@RequestPart("file") MultipartFile multipartFile) {
        // 文件目录
        String filename = multipartFile.getOriginalFilename();
        String filepath = String.format("/test/%s", filename);
        File file = null;
        try {
            // 上传文件
            file = File.createTempFile(filepath, null);
            multipartFile.transferTo(file);
            String key = cosManager.putObject(file, filename);
            // 返回可访问地址
            log.info("上传成功，图片名字为：{}", key);
            return ResultUtils.success(key);
        } catch (Exception e) {
            log.error("file upload error, filepath = " + filepath, e);
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "上传失败");
        } finally {
            if (file != null) {
                // 删除临时文件
                boolean delete = file.delete();
                if (!delete) {
                    log.error("file delete error, filepath = {}", filepath);
                }
            }
        }
    }

    /**
     * 删除Cos图片
     *
     * @param url 图片地址
     * @return 删除结果
     */
    @PostMapping("/test/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @Operation(summary = "测试删除接口")
    public BaseResponse<Boolean> delete(String url) {
        log.info("删除图片:{}", url);
        if (url == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        try {
            cosManager.deleteObject(url);
            return ResultUtils.success(true);
        } catch (Exception e) {
            log.info("cos file delete error, filepath = {} :{}", url, e.toString());
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "删除cos文件失败");
        }
    }

}
